package ar.edu.itba.pod.legajo45129.simul.communication.factory;

import java.io.Serializable;

import ar.edu.itba.pod.legajo45129.simul.communication.payload.DisconnectPayloadImpl;
import ar.edu.itba.pod.legajo45129.simul.communication.payload.NodeAgentLoadRequestPayloadImpl;
import ar.edu.itba.pod.simul.communication.MarketData;
import ar.edu.itba.pod.simul.communication.Message;
import ar.edu.itba.pod.simul.communication.MessageType;
import ar.edu.itba.pod.simul.communication.payload.DisconnectPayload;
import ar.edu.itba.pod.simul.communication.payload.NodeAgentLoadPayload;
import ar.edu.itba.pod.simul.communication.payload.Payload;
import ar.edu.itba.pod.simul.market.NodeMarketDataPayloadImpl;
import ar.edu.itba.pod.simul.market.Resource;

public class MessageFactory implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1870334915114023031L;

	public static boolean isForwardable(Message message) {
		MessageType[] forwardableMsg = {MessageType.DISCONNECT, MessageType.RESOURCE_REQUEST};
		for (MessageType mt: forwardableMsg) {
			if (mt.equals(message.getType())) {
				return true;
			}
		}
		
		return false;
	}
	
	public static Message copy(Message message) {
		return new Message(message.getNodeId(), 
				message.getTimeStamp(), message.getType(), message.getPayload());
	}
	
//	NODE_AGENTS_LOAD, 				// Node agents load info
//	NODE_AGENTS_LOAD_REQUEST,		// Node agents load info request to all nodes
//	NODE_MARKET_DATA,				// Node market data
//	NODE_MARKET_DATA_REQUEST,		// Node market data request to all nodes
//	DISCONNECT, 
//	RESOURCE_TRANSFER, 				// Resource been transfer from one node to another node
//	RESOURCE_TRANSFER_CANCELED,		// Resource transfer from one node to another node canceled
//	RESOURCE_REQUEST,				// Resource request from one node to the cluster
//	NEW_MESSAGE_REQUEST,			// New messages request from one node to another node
//	NEW_MESSAGE_RESPONSE			// New messages response from one node to another node
	
	public static Message NodeAgentsLoad(String nodeId, NodeAgentLoadPayload payload) {
		Message ret = new Message(nodeId, System.currentTimeMillis(), 
				MessageType.NODE_AGENTS_LOAD, payload);		
		return ret;
	}
	
	public static Message NodeAgentsLoadRequest(String nodeId) {
		Message ret = new Message(nodeId, System.currentTimeMillis(), 
				MessageType.NODE_AGENTS_LOAD_REQUEST, new NodeAgentLoadRequestPayloadImpl());
		return ret;
	}
	
	public static Message Disconnect(String fromNode, String disconnectNode) {
		DisconnectPayload dp = new DisconnectPayloadImpl(disconnectNode);
		Message ret = new Message(fromNode, System.currentTimeMillis(), 
				MessageType.DISCONNECT, dp);
		return ret;
	}
	
	public static Message ResourceTransfer(String fromNode, Payload payload) {
		Message ret = new Message(fromNode, System.currentTimeMillis(),
				MessageType.RESOURCE_TRANSFER, payload);
		return ret;
	}

	public static Message ResourceRequestMessage(String fromNode,
			Resource resource, int amount) {
		Message ret = new Message(fromNode, System.currentTimeMillis(),
				MessageType.RESOURCE_REQUEST, new ResourceRequestPayloadImpl(resource, amount));
		return ret;
	}

	public static Message NodeMarketDataRequest(String fromNode) {
		Message ret = new Message(fromNode, System.currentTimeMillis(), 
				MessageType.NODE_MARKET_DATA_REQUEST, new NodeMarketDataRequestPayloadImpl());
		return ret;
	}

	public static Message NodeMarketData(String fromNode,
			MarketData marketData) {
		Message ret = new Message(fromNode, System.currentTimeMillis(), 
				MessageType.NODE_MARKET_DATA, new NodeMarketDataPayloadImpl(marketData));
		return ret;
	}
	

}
